<?php
##############################################
#
#   分类管理
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\CategoryModel;
use Admin\Model\AdminBaseModel;

class CategoryController extends AdminBaseController {
	
	protected $_categoryObject;
	
	public function _initialize(){
		parent::_initialize();
		$this -> _categoryObject  = new CategoryModel();
	}
	
	/**
	 * 分类列表
	 */
	public function categoryList(){
		if(IS_GET){
			$category = $this -> _categoryObject -> getCategory();
			$data = $this -> _categoryObject -> disData($category);
			$this -> assign('category', $data);
			$this -> display('category_list');
		}
	}
	
	/**
	 * 添加分类
	 */
	public function addCategory(){
		$data = I('post.');
		$data['pid'] = intval($data['pid']);
		$data['sort'] = intval($data['sort']);
		$data['is_display'] = intval($data['is_display']);
		$admin_base_model = new AdminBaseModel('category');
		$result = $admin_base_model -> addData($data);
		if($result)
			$this -> success('添加成功', U('categoryList'), 1);
		else
			$this -> error('操作失败', '', 1);
	}
	
	/**
	 * 删除分类
	 */
	public function deleteCategory(){
		if(!IS_AJAX) return;
		$result = $this -> _categoryObject -> deleteCategory(intval($_GET['cid']));
		if($result == 1)
			$this -> ajaxReturn(array('status' => 1,'message' => '该分类有子分类，不可删除'));
		elseif($result == 2)
			$this -> ajaxReturn(array('status' => 2,'message' => '删除成功'));
		elseif($result == 3)
			$this -> ajaxReturn(array('status' => 3,'message' => '操作失败'));
	}
	
}